﻿$(function () {
    $('#tt').datagrid({
        loadMsg: "LoadIng...",
        iconCls: 'icon-reload',
        url: '',
        idField: 'TradeOrderID',
        autoRowHeight: false,
        fitColumns: false,
        scrollbarSize: 0,
        pageSize: 20,
        sortable: true,
        striped: false,      //隔行变色
        singleSelect: false, //是否单选
        pagination: true,   //分页控件
        rownumbers: true,
        columns: [[
            { field: 'TradeOrderID', checkbox: true },
            { field: 'Opt', title: 'Opt', width: 230, align: 'center', formatter: ColumnOpt },
            { field: 'TradeName', title: 'TradeType' },
            { field: 'TradeType', hidden: true },
            { field: 'RState', hidden: true },
            { field: 'BackgroundColor', hidden: true },
            { field: 'RStateName', title: 'RState', width: 80 },
            { field: 'TradeOrderNO', title: 'OrderNO', width: 140, sortable: true },
            { field: 'TradeDate', title: 'TradeDate', width: 140, sortable: true },
            { field: 'RecvTypeName', title: 'RecvTypeName', width: 120 },
            { field: 'SourceOrderNO', title: 'SourceOrderNO', sortable: true },
            { field: 'FromWarehouseID', hidden: true },
            { field: 'WarehouseName', title: 'WarehouseName', width: 80 },
            { field: 'ApproveDate', title: 'ApproveDate', width: 140, sortable: true },
            { field: 'ApproveUser', title: 'ApproveUser', width: 100 },
            { field: 'CreateTime', title: 'CreateTime', width: 140, sortable: true },
            { field: 'CreateUser', title: 'CreateUser', width: 100 }

        ]],
        rowStyler: function (index, row) {
            return row.BackgroundColor;
        },
        onLoadSuccess: function (data) {
            $('#tt').datagrid("fillRows");
            $('#tt').datagrid('resize');
        },
        onSortColumn: function (sort, order) {
            $("#hSort").val(sort);
            $("#hOrder").val(order);
        }
    });
    $(window).resize(function () {
        $('#tt').datagrid('resize');
    });
    $(".listmore").click(function () {
        $(".flistdis").toggle();
    });
})
function IndexOpen(rows) {
    OpenWindow("TradeOrderEdit", "WMS/UI/WmsPurchaseReceiptOrderEdit.aspx", 1200, 650, function () {
        loadLocal();
    });
}
//查询
function loadLocal() {
    var TradeOrderNO = $("#txtTradeOrderNO").val();
    /* var PartnerNO = $("#txtPartnerNO").val();*/
    var BeginTime = $("#BeginTime").val();
    var EndTime = $("#EndTime").val();
    //排序字段
    var sort = $("#hSort").val();
    var order = $("#hOrder").val();
    $("#tt").datagrid({
        url: '/api/WMS/QueryWmsTradeOrderInfo',
        method: 'Get',
        queryParams: {
            TradeOrderNO: TradeOrderNO,
            BeginTime: BeginTime,
            EndTime: EndTime,
            sort: sort,
            order: order,
            TradeType: 0
        },
        loadFilter: function (data) {
            return JSON.parse(data);
        }
    });
    $('#tt').datagrid('clearSelections');
}

//删除
function Delet() {
    var rows = $('#tt').datagrid('getSelections');
    if (rows.length < 1) {
        Alert("Please select！");
        return false;
    }
    for (var i = 0; i < rows.length; i++) {
        if (rows[i].RState != "0") {
            Alert("Order:" + rows[i].TradeOrderNO + "Not in the new creation state, unable to delete, please review immediately！");
            return false;
        }
    }
    Confirm("Confirm to delete" + rows.length + "Is it a receipt？", function () {
        var MateialInfo = fnSelectList('tt', rows);
        var url = '/api/WMS/DeleteWmsTradeOrderInfo';
        DataService.PostJson(url, MateialInfo, function (error, response) {
            if (error) {
                Notice("success!");
            } else {
                Alert(response);
            }
            loadLocal();
        })
    });
}

//审核
function Check() {
    var rows = $('#tt').datagrid('getSelections');

    if (rows.Length < 1) {
        Alert("Please select！");
        return false;
    }
    for (var i = 0; i < rows.length; i++) {
        if (rows[i].RState != "0") {
            Alert("Order:" + rows[i].TradeOrderNO + "Not in review status, unable to Check ！");
            return false;
        }
    }
    Confirm("Are you sure you want to Check？", function () {
        var MateialInfo = fnSelectList('tt', rows);
        var url = '/api/WMS/CheckWmsTradeOrderInfo';
        DataService.PostJson(url, MateialInfo, function (error, response) {
            if (error) {
                Notice("success");
            } else {
                Alert(response);
            }
            loadLocal();
        })
    });
}
//反审核
function UpCheck() {
    var rows = $('#tt').datagrid('getSelections');
    if (rows.Length < 1) {
        Alert("Please select！");
        return false;
    }
    for (var i = 0; i < rows.length; i++) {
        if (rows[i].RState != "1") {
            Alert("Order:" + rows[i].TradeOrderNO + "Not in review status, unable to reverse audit！");
            return false;
        }
    }
    Confirm("Are you sure you want to Return？", function () {
        var MateialInfo = fnSelectList('tt', rows);
        var url = '/api/WMS/UpCheckWmsTradeOrderInfo';
        DataService.PostJson(url, MateialInfo, function (error, response) {
            if (error) {
                Notice("success");
            } else {
                Alert(response);
            }
            loadLocal();
        })
    });
}

function myformatter(date) {
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    var d = date.getDate();
    return y + '-' + (m < 10 ? ('0' + m) : m) + '-' + (d < 10 ? ('0' + d) : d);
}
function myparser(s) {
    if (!s) return new Date();
    var ss = (s.split('-'));
    var y = parseInt(ss[0], 10);
    var m = parseInt(ss[1], 10);
    var d = parseInt(ss[2], 10);
    if (!isNaN(y) && !isNaN(m) && !isNaN(d)) {
        return new Date(y, m - 1, d);
    } else {
        return new Date();
    }
}
//根据状态改变值color
//function setColumnForeColor(value, row, index) {
//    if (value == '新建' || value == 'Editing') {
//        //红色
//        return '<span style="color:gray;">' + value + '</span>';
//    }
//    if (value == '已审核' || value == 'Approved') {
//        //绿色
//        return '<span style="color:Green;">' + value + '</span>';
//    }
//    if (value == '登账' || value == 'Account') {
//        return '<span style="color:#07C160;">' + value + '</span>';
//    }
//    if (value == '删除' || value == 'Delete') {
//        //灰色
//        return '<span style="color:red;">' + value + '</span>';
//    }
//    return value;
//}

function ColumnOpt(value, row) {
    var temp = JSON.stringify(row);
    return "<a name='lock1' href='#' style='color:#a61c00' data-row='" + encodeURIComponent(temp) + "' onclick='Update(this);'>[Update]</a> &nbsp; | &nbsp;" +
        "<a name='lock2' href='#' style='color:#666666' data-row='" + encodeURIComponent(temp) + "' onclick='View(this);'>[Review]</a> &nbsp; | &nbsp;" +
        "<a name='lock3' href='#' style='color:#07C160' data-row='" + encodeURIComponent(temp) + "' onclick='Account(this);'>[Account]</a>";
}
//浏览
function View(element) {
    var temp = decodeURIComponent(element.getAttribute('data-row'));
    var row = JSON.parse(temp);
    const params = encodeURIComponent(JSON.stringify(row));
    OpenWindow("TradeOrderEdit", "WMS/UI/WmsPurchaseReceiptOrderEdit.aspx?params=" + params + "&view=view", 1200, 650, function () {
        loadLocal();
    });
}
//修改
function Update(element) {
    var temp = decodeURIComponent(element.getAttribute('data-row'));
    var row = JSON.parse(temp);
    if (row.RState != "0") {
        Notice("Not in the newly created state, unable to modify");
        return false;
    }
    const params = encodeURIComponent(JSON.stringify(row));
    OpenWindow("TradeOrderEdit", "WMS/UI/WmsPurchaseReceiptOrderEdit.aspx?params=" + params, 1200, 650, function () {
        loadLocal();
    });
}
//登账
function Account(element) {
    var temp = decodeURIComponent(element.getAttribute('data-row'));
    var row = JSON.parse(temp);
    // 确保对话框已初始化
    if (!$('#accountDialog').data('dialog')) {
        $('#accountDialog').dialog({
            closed: true,
            resizable: true
        });
    }

    // 设置值并打开
    $('#ATradeOrderNO').textbox('setValue', row.TradeOrderNO);
    $('#accountDialog').dialog('open');
}

function AccuntSue() {
    var temp = decodeURIComponent(element.getAttribute('data-row'));
    var row = JSON.parse(temp);
    if (row.RState != "1") {
        Notice("Not in audit status, unable to Account!");
        return false;
    }
    Confirm("Are you sure you want to Account？", function () {
        var url = '/api/WMS/AccountWmsTradeOrderInfo';
        DataService.PostJson(url, row, function (error, response) {
            if (error) {
                Notice("success");
            } else {
                Alert(response);
            }
            loadLocal();
        })
    });
}